package com.wjw.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.wjw.entity.Users;
import com.wjw.vo.UsersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjw.entity.Business;
import com.wjw.dao.BusinessMapper;

/**
 * @author WuJianWei
 * @date 2022/3/3 9:06
 */
@Service
public class BusinessService extends ServiceImpl<BusinessMapper, Business> {
    @Autowired
    private UsersService usersService;


    public Business getByUserId(Integer id) {
        LambdaQueryWrapper<Business> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Business::getUserId, id);
        Business business = baseMapper.selectOne(queryWrapper);
        return business;
    }

    public Boolean modify(UsersVo vo) {

        Business business = new Business();
        business.setBusinessId(vo.getBusinessId());
        business.setBusinessPhone(vo.getBusinessPhone());
        business.setBusinessName(vo.getBusinessName());
        int i = baseMapper.updateById(business);
        if (i > 0) {
            return true;
        }
        return false;
    }


    public Boolean addBusiness(String businessName, String businessPhone, Integer userId) {
        long id = IdWorker.getId();
        Business business = new Business();
        business.setBusinessId(id);
        business.setBusinessName(businessName);
        business.setBusinessPhone(businessPhone);
        business.setUserId(userId);
        int i = baseMapper.insert(business);
        if (i > 0) {
            Users users = new Users();
            users.setUserId(userId);
            users.setBusinessUser(true);
            usersService.updateUser(users);
            return true;
        }
        return false;
    }

}
